package com.cai.dao.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cai.dao.DbUtil;
import com.cai.dao.MessageDao;
import com.cai.pojo.TopicHelp;

import java.util.List;

/**
 * @author RuoLi
 */
public class MessageDaoImpl implements MessageDao {
    @Override
    @SuppressWarnings("unchecked")
    public String selectAllMessage() {
        String sql = "select * from topic_help";
        DbUtil dbUtil = new DbUtil();
        List<TopicHelp> list = (List<TopicHelp>) dbUtil.selectContent(sql, new String[]{}, TopicHelp.class);
        JSONArray jsonArray = new JSONArray();
        for (TopicHelp topic : list
        ) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("tipId", topic.getTip_id());
            jsonObject.put("tipTitle", topic.getTip_title());
            jsonObject.put("tipContext", topic.getTip_context());
            jsonObject.put("tipStatus", topic.getTip_status());
            jsonArray.add(jsonObject);
        }
        return jsonArray.toJSONString();
    }

    @Override
    public int insertMessage(TopicHelp topicHelp) {
        int status;
        String sql = "insert into  topic_help(tip_title,tip_context,tip_status) values(?,?,?)";
        status = DbUtil.updateContent(sql, new Object[]{topicHelp.getTip_title(), topicHelp.getTip_context(), 0});
        return status;
    }

    @Override
    public int deleteMessage(int tipId) {
        int status;
        String sql = "delete from topic_help where tip_id=?";
        status = DbUtil.updateContent(sql, new Object[]{tipId});
        return status;
    }
}
